//
//  HTStatusOriginalView.swift
//  mySinaBlog
//
//  Created by whtapple on 3/26/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class HTStatusOriginalView: UIView {
    //底部约束
    var bottomCon: Constraint?
  //状态数据
    var status:HTStatus?{
        didSet{
            // 设置头像
            if let icon = status?.user?.profile_image_url{
                let url = NSURL(string: icon)!
//                iconView.setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            //设置名称
            nameLabel.text = status?.user?.screen_name
            //设置时间
            timeLabel.text = status?.created_at
            //来源
            sourceLabel.text = status?.source
            //内容
            contentLabel.text = status?.text
            //认证图标
            verifiedView.image = status?.user?.verified_typeImage
            //会员等级
            vipiconView.image = status?.user?.mbrankImage
            //判断图片 view 是否隐藏，并且改变约束
            bottomCon?.uninstall()
            if let picURLs = status?.storePic{
                myimageView.hidden = false
                myimageView.pictureURLs = picURLs
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCon = make.bottom.equalTo(myimageView).constraint
                })
            }else{
                myimageView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCon = make.bottom.equalTo(contentLabel).constraint
                })
            }
            
        }
        
    }
    ///初始化的时候加载子控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
        backgroundColor = UIColor.whiteColor()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///准备 UI 添加子控件
    private func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipiconView)
        addSubview(verifiedView)
        addSubview(contentLabel)
         addSubview(myimageView)
        ///添加约束
        //头像约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(margin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
         //名字约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(margin)
            make.top.equalTo(iconView)
        }
         //会员等级图标约束
        vipiconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(margin)
            make.top.equalTo(iconView)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
         //认证约束
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
             make.size.equalTo(CGSize(width: 17, height: 17))
        }
         //时间约束
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_bottom).offset(margin)
            make.leading.equalTo(iconView.snp_trailing).offset(margin)
        }
         //来源约束
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing)
        }
         //内容约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            make.leading.equalTo(self).offset(margin)
            make.trailing.equalTo(self).offset(-margin)
        }
         //图片约束
        myimageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            make.leading.equalTo(contentLabel)
           make.size.equalTo(CGSize(width: 200, height: 200))
        }
         //约束
        self.snp_makeConstraints { (make) -> Void in
             self.bottomCon = make.bottom.equalTo(myimageView).offset(margin).constraint
        }
    }
   //MARK: -懒加载
    //用户头像
//    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
      private lazy var iconView: UIImageView = UIImageView()

    //用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
//        label.text = "sdkjfijfoasjdf"
        label.font = UIFont.systemFontOfSize(bigfontsize)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    /// 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
//        label.text = "2016.1.20"
        label.font = UIFont.systemFontOfSize(smallfontsize)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    /// 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
//        label.text = "凤凰网"
        label.font = UIFont.systemFontOfSize(smallfontsize)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    /// 会员等级
    private lazy var vipiconView: UIImageView = UIImageView()
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView()

    /// 原创微博内容
    private lazy var contentLabel: UILabel = {
       let label = UILabel()
//        label.text = "面朝大海，春暖花开"
        label.font = UIFont.systemFontOfSize(bigfontsize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * 8
        return label
    }()
     private lazy var myimageView: HTImagesView = HTImagesView()

}
